import { createRouter, createWebHistory } from 'vue-router/dist/vue-router'
import RootView from "../views/RootView.vue"
import pinia from '../stores'
import useUserInfoStore from '../stores/userInfo'

const userInfoStore = useUserInfoStore(pinia)

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      component: RootView,
      redirect: "/home",
      children: [
        {
          path: '/home',
          component: () => import('../views/home/Index.vue')
        },
        {
          path: '/club',
          component: () => import('../views/club/Index.vue')
        },
        {
          path: '/orders',
          component: () => import('../views/orders/Index.vue')
        },
        {
          path: '/mine',
          component: () => import('../views/mine/Index.vue')
        },
        {
          path: "/address",
          component: () => import('../views/mine/address/AddressList.vue')
        },
        {
          path: '/addressEdit',
          name: 'addressEdit',
          component: () => import('../views/mine/address/AddressEdit.vue')
        }
      ]
    },
    {
      path: "/login",
      component: () => import('../views/login/Index.vue')
    }
  ]
})

// 添加路由守卫
router.beforeEach((to, from, next) => {

  if (to.path == '/login' || userInfoStore.myUserInfo) {
    next()
  } else {
    next('/login')
  }
})

export default router
